﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace information.Resources
{
    public partial class EditData : Form
    {
        private DataTable DT = new DataTable();
        private SqlDataAdapter SDA = new SqlDataAdapter();

        public EditData()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //连接数据库
            string strcon;
            strcon = "Server=192.168.0.88; Database=information;User id ='sa';pwd=123456";  //information            
            SqlConnection myon = new SqlConnection(strcon);
            myon.Open();
            MessageBox.Show("数据库连接成功", "数据库");

            //数据从数据库中读取
            string mysql = "select * from InformationTable";                //Sql查询语句
            SqlDataAdapter myada = new SqlDataAdapter(mysql, myon);   //定义OleDbDataAdapter对象实例并连接数据库表
            DataSet myds = new DataSet();      //定义DataSet对象实例  
            myada.Fill(myds);                 //填充数据集
            dataGridView1.DataSource = myds.Tables[0].DefaultView;   //连接数据表格，显示数据
            myon.Close();    //关闭OleDbConnection对象实例
            myon.Dispose();   //释放OleDbConnection对象实例所占资源空间
            
        }

        private void button2_Click(object sender, EventArgs e)
        {  SqlDataAdapter ada;
           DataSet ds;
           string strcon;
           strcon = "Server=192.168.0.88; Database=information;User id ='sa';pwd=123456";  //information            
           SqlConnection myon = new SqlConnection(strcon);
           myon.Open();
           MessageBox.Show("数据库连接成功", "数据库");
            string mystr = "select name,sex,time from InformationTable order by time";
            ada = new SqlDataAdapter(mystr, myon);
            ds = new DataSet();
            ada.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            myon.Close();    //关闭OleDbConnection对象实例
            myon.Dispose();   //释放OleDbConnection对象实例所占资源空间
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlDataAdapter ada;
            DataSet ds;
            string strcon;
            strcon = "Server=192.168.0.88; Database=information;User id ='sa';pwd=123456";  //information            
            SqlConnection myon = new SqlConnection(strcon);
            myon.Open();
            MessageBox.Show("数据库连接成功", "数据库");
            string mystr = "select name,sex,time from InformationTable order by time desc";
            ada = new SqlDataAdapter(mystr, myon);
            ds = new DataSet();
            ada.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            myon.Close();    //关闭OleDbConnection对象实例
            myon.Dispose();   //释放OleDbConnection对象实例所占资源空间
        }

        private void EditData_Load(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection("Server=192.168.0.88; Database=information;User id ='sa';pwd=123456");
            SqlCommand SCD = new SqlCommand("select * from InformationTable", conn);
            SDA.SelectCommand = SCD;
            SDA.Fill(DT);
            dataGridView1.DataSource = DT;
        }

        private void button4_Click(object sender, EventArgs e)
        {  
            try
            {
                SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);                
                SDA.Update(DT);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
            MessageBox.Show("更新成功!");
        }

        }


 }

    

